<?php

namespace app\api\controller;

use app\common\model\Member;

class Base
{

    public function login()
    {
        //数据校验
        $mobile = post_param('mobile');
        $password = post_param('password');
        if (empty($mobile) || empty($password)) output_error_param();
        $memberModel = new Member();
        //判断用户名与密码是否匹配
        $condition = array();
        $condition[] = ['member_mobile', '=', $mobile];
        $condition[] = ['member_password', '=', md5_encrypt($mobile, $password)];
        $data = $memberModel->where($condition)->find();
        if (empty($data)) output_error('账号或密码错误~');
        //登录成功后的操作,生成token,存好，返回给用户
        $token = md5_encrypt(time(), $data['member_id']);
        //补全代码，更新数据库，输出token
        $updata = array();
        $updata['member_token'] = $token;
        $memberModel->where('member_id', '=', $data['member_id'])->update($updata);
        output_data($token);
    }

    public function register()
    {
        $mobile = post_param('mobile');
        $password = post_param('password');
        if (empty($mobile) || empty($password)) output_error_param();
        $memberModel = new Member();
        $data = $memberModel->where('member_mobile', '=', $mobile)->find();
        if (!empty($data)) output_error('账号已存在');
        $data = array();
        $data['member_mobile'] = $mobile;
        $data['member_password'] = md5_encrypt($mobile, $password);
        $data['member_name'] = '用户' . $mobile;
        $data['add_time'] = time();
        if ($memberModel->insert($data)) output_success();
        output_error('注册失败');
    }

}
